﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ACCPS1ProjectDemo
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //关闭窗口
            this.Close();
        }
        DBHelper helper = new DBHelper();
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                string sql = string.Format("select count(*) from Admin where loginId='{0}' and loginPwd='{1}'",
                    txtName.Text,txtPwd.Text);
                int result =(int)helper.ExecuteScalar(sql);
                if (result==1)  //验证成功
                {
                    //记录登录信息（用户名和密码）,以便在其它窗体中使用
                    //请参见Common.cs文件
                    Common.UserName = txtName.Text;
                    Common.Password = txtPwd.Text;
                    //显示主窗体
                    MainForm form = new MainForm();
                    form.Show();
                    this.Hide(); //当前（登录）窗口隐藏
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                    txtName.Text = "";
                    txtPwd.Text = "";
                    txtName.Focus();
                }
            }
        }

        /// <summary>
        /// 检测输入信息是否填写完整
        /// </summary>
        /// <returns></returns>
        private bool CheckInput()
        {
            if (txtName.Text.Trim().Length==0  || txtPwd.Text.Trim().Length==0)
            {
                MessageBox.Show("请填写登录名和密码！");
                return false;
            }
            return true;
        }
    }
}
